package common.window
{
	import flash.events.Event;
	
	import fairygui.Controller;
	import fairygui.GButton;
	import fairygui.GRoot;
	import fairygui.RelationType;
	import fairygui.UIPackage;
	import fairygui.Window;
	
	public class AlertDialog extends Window
	{
		private var confirmLabel:String="确定";
		private var cancelLabel:String="取消";
		private var callBack:Function;
		private var confirmBtn:GButton;
		private var cancelBtn:GButton;
		private var closeButton:GButton;
		public function AlertDialog()
		{
			super();
			this.contentPane = UIPackage.createObject("Builder","AlertDialog").asCom;
			this.modal = true;
			contentPane.removeRelation(this,RelationType.Size);
			confirmBtn=contentPane.getChild("btnOK").asButton;
			cancelBtn=contentPane.getChild("btnNo").asButton;
			closeButton=contentPane.getChild("closeButton").asButton;
			
			confirmBtn.addClickListener(buttonClick);
			cancelBtn.addClickListener(buttonClick);
			closeButton.addClickListener(closeEventHandler);
		}
		private function buttonClick(e:Event):void{
			var btn:GButton=e.target as GButton;
			if(callBack!=null){
				if(btn.text==confirmLabel){
					callBack(1);
				}else{
					callBack(0);
				}
			}
			closeEventHandler(e);
		}
		
		override public function dispose():void
		{
			super.dispose();
			confirmBtn.removeClickListener(buttonClick);
			cancelBtn.removeClickListener(buttonClick);
			closeButton.removeClickListener(closeEventHandler);
			confirmLabel=cancelLabel=null;
			callBack=null;
			confirmBtn=cancelBtn=closeButton=null;
		}
		public function open(root:GRoot,text:String,confirmLabel:String=null,cancelLabel:String=null,callBack:Function = null):void
		{
			showOn(root);
			if(!text)
			{
				text = "";
			}
			else 
			{
				text = text.replace(/\r\n/g,"\n");
				text = text.replace(/\r/g,"\n");
			}
			this.callBack=callBack;
			this.contentPane.getChild("label").text = text;
			this.setSize(contentPane.width,contentPane.height);
			this.centerOn(root,true);

			
			var c:Controller=this.contentPane.getController("c1");
			if(confirmLabel!=null&&cancelLabel!=null){
				c.selectedIndex=1;
			}else{
				c.selectedIndex=0;
			}
			if(confirmLabel){
				this.confirmLabel=confirmLabel;
				confirmBtn.text=confirmLabel;
			}
			if(cancelLabel){
				this.cancelLabel=cancelLabel;
				cancelBtn.text=cancelLabel;
			}
		}
		public static function open(root:GRoot,text:String,confirmLabel:String=null,cancelLabel:String=null,callBack:Function = null):void{
			var alert:AlertDialog=new AlertDialog();
			alert.open(root,text,confirmLabel,cancelLabel,callBack);
		}
	}
}